package com.herench.bunch.lab.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.group.ORU_R01_OBSERVATION;
import ca.uhn.hl7v2.model.v24.message.ORR_O02;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.springframework.util.Assert;

import java.util.*;


/**
 * @author luoxiaoming on 18-10-16 下午1:14
 * @email luoxiaoming@herench.com
 */
public class LabReportConvert extends AbstractMessageConvert {

    @Override
    public Object toMessage(RequestMessage requestMessage) throws Exception {
        List<Map<String, Object>> reports = new ArrayList<>();
        ORU_R01 hl7Message = (ORU_R01) requestMessage.getHl7Message();
        OBR obr = hl7Message.getPATIENT_RESULT().getORDER_OBSERVATION().getOBR();
        String testNo = obr.getObr1_SetIDOBR().getValue();
        String ordId = obr.getObr2_PlacerOrderNumber().getEi1_EntityIdentifier().getValue();
        String resultDateTime = obr.getResultsRptStatusChngDateTime().getTs1_TimeOfAnEvent().getValue();


        List<ORU_R01_OBSERVATION> observations = hl7Message.getPATIENT_RESULT().getORDER_OBSERVATION(0).getOBSERVATIONAll();
        if (observations != null && !observations.isEmpty()) {
            for (ORU_R01_OBSERVATION obn : observations) {
                Map<String, Object> el = new HashMap<>();
                el.put("testNo", testNo);
                el.put("ordId", ordId);
                el.put("resultDateTime", resultDateTime);
                el.put("lowerLimit","");
                el.put("upperLimit","");
                OBX obx = obn.getOBX();


                String itemNo = obx.getObx1_SetIDOBX().getValue();
                el.put("itemNo", itemNo);

                String reporter = obx.getObx16_ResponsibleObserver().getXcn1_IDNumber().getValue();
                el.put("reporter", reporter);

                String reporterName = obx.getObx16_ResponsibleObserver().getXcn3_GivenName().getValue();
                el.put("reporterName", reporterName);

                String refRange = obx.getReferencesRange().getValue();
                el.put("refRange", refRange);

                String reportItemCode = obx.getObx3_ObservationIdentifier().getCe1_Identifier().getValue();
                el.put("reportItemCode", reportItemCode);

                String reportItemCode2 = obx.getObx3_ObservationIdentifier().getCe4_AlternateIdentifier().getValue();
                el.put("reportItemCode2", reportItemCode2);

                String reportItemName = obx.getObx3_ObservationIdentifier().getCe2_Text().getValue();
                el.put("reportItemName", reportItemName);

                String reportItemName2 = obx.getObx3_ObservationIdentifier().getCe5_AlternateText().getValue();
                el.put("reportItemName2", reportItemName2);

                String instrumenId = obx.getEquipmentInstanceIdentifier(0).getEi1_EntityIdentifier().getValue();
                el.put("instrumenId", instrumenId);

                String abnormalIndicator = obx.getObx8_AbnormalFlags().getValue();
                el.put("abnormalIndicator", abnormalIndicator);

                String units = obx.getUnits().getCe1_Identifier().getValue();
                el.put("units", units);

                String result = obx.getObservationResultStatus().getValue();
                el.put("result", result);

                String printOrder = obx.getNatureOfAbnormalTest().getValue();
                el.put("printOrder", printOrder);
                reports.add(el);
            }
        }

        return reports;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        ORU_R01 oru_r01 = new ORU_R01();
        oru_r01.initQuickstart("ORU_R01", "ORU_R01", "A");
        MSH msh = oru_r01.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("900020");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("LIS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        OBR obr = oru_r01.getPATIENT_RESULT().getORDER_OBSERVATION().getOBR();
        obr.getObr1_SetIDOBR().setValue("M1215100000008");
        obr.getObr2_PlacerOrderNumber().getEi1_EntityIdentifier().setValue("orderId");
        obr.getRequestedDateTime().getTs1_TimeOfAnEvent().setValue("requestedDateTime");
        obr.getResultsRptStatusChngDateTime().getTs1_TimeOfAnEvent().setValue("resultDateTime");
        obr.getDangerCode().getIdentifier().setValue("1");//危急值类型
        obr.getDangerCode().getText().setValue("19");

        OBX obx1 = oru_r01.getPATIENT_RESULT().getORDER_OBSERVATION(0).getOBSERVATION(0).getOBX();

        obx1.getObx1_SetIDOBX().setValue("itemNo");
        obx1.getObx16_ResponsibleObserver().getXcn1_IDNumber().setValue("reporter");
        obx1.getObx16_ResponsibleObserver().getXcn3_GivenName().setValue("reporterName");
        obx1.getReferencesRange().setValue("refRange");
        obx1.getObx3_ObservationIdentifier().getCe1_Identifier().setValue("reportItemCode");
        obx1.getObx3_ObservationIdentifier().getCe4_AlternateIdentifier().setValue("reportItemCode2");
        obx1.getObx3_ObservationIdentifier().getCe5_AlternateText().setValue("reportItemName2");
        obx1.getObx3_ObservationIdentifier().getCe2_Text().setValue("reportItemName");
        obx1.getEquipmentInstanceIdentifier(0).getEi1_EntityIdentifier().setValue("instrumenId");
        obx1.getObx8_AbnormalFlags().setValue("abnormalIndicator");
        obx1.getUnits().getCe1_Identifier().setValue("units");
        obx1.getObservationResultStatus().setValue("result");
        obx1.getNatureOfAbnormalTest().setValue("printOrder");

        return oru_r01;
    }

    @Override
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        Map<String, Object> res = (Map<String, Object>) message;
        ORR_O02 orr_o02 = new ORR_O02();
        orr_o02.initQuickstart("ORR_OO2", "ORR_OO2", "A");
        MSH msh = orr_o02.getMSH();
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("HIS");
        msh.getMsh5_ReceivingApplication().getHd1_NamespaceID().setValue("LIS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("900020");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());

        MSA msa = orr_o02.getMSA();
        msa.getMsa6_ErrorCondition().getCe1_Identifier().setValue(String.valueOf(res.get("ret")));
        if (String.valueOf(res.get("ret")).equals("0")) {
            msa.getMsa6_ErrorCondition().getCe2_Text().setValue("接收成功");
        }else{
            msa.getMsa6_ErrorCondition().getCe2_Text().setValue("接收失败");
        }
        return orr_o02;
    }
}
